<?php
/**
 * Search a Company -
 *
 * @package Hive\Company
 * @author Xavier VILAIN
 *
 */
use Fox\Dao\SqlFragment;
use Fox\Html\Input;
use Fox\Html\Screen;
use Hive\Company;
use Nawa\MedicalStudy;
use Fox\Html\Button;

/*
//@TODO think of moving it to a search engine
$searchScreen = new Fox\Html\Screen\Search\Basic('search',new Company(),array('company_name' => false),100);
$searchScreen->screen()->pageTitle(Msg(Hive::BUNDLE,Hive::COMPANY_SEARCH_FOR));
$searchScreen->grid(Xview::instance()->component(Company::BUNDLE,'result'));
*/
$searchScreen = new Screen('sSearch','vlook');
$searchScreen
	->pageTitle(Msg(Hive::BUNDLE,Hive::COMPANY_SEARCH_FOR))
		->block()
		->add(new Input('company'))->parentObject()
		->add(new Input('dbcnt',true,100));
if($searchScreen->submitted()) {
	$fields = $searchScreen->get();
	if(!empty($fields['company'])) {
		$cleanWords = Company::cleanup($fields['company']);
		$cleanWords = '%'.str_replace(' ','%',$cleanWords).'%';
		$fragments[] = new SqlFragment('company_keyname LIKE ?',array($cleanWords));
	}
	if(!$searchScreen->errors()) {
		$query = Fox\Dao::infos(Company::className())
					->handle()->createQueryBuilder();
		$query->select('compid','company_name','uname','udate')
				->from('companies','cp')
				->setMaxResults($fields['dbcnt']);
				
		$fragments[] = new SqlFragment('loekz = ?',array(0));
		$fragment = SqlFragment::combine($fragments);
		$data = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
		$grid = Xview::instance()->component(Company::BUNDLE,'result');
		$grid->initialize($data);
	}
}
echo $searchScreen->render();
if(isset($grid)) {
	echo $grid->display();
}